package com.ssd.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssd.entity.RegionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

@Mapper
public interface RegionDao extends BaseMapper<RegionEntity> {

    @Select("<script>" +
            "SELECT r.id, r.name, r.remark, COUNT(n.id) AS node_count " +
            "FROM tb_region r " +
            "LEFT JOIN tb_node n ON r.id = n.region_id " +
            "<where> " +
            "<if test='regionName != null and regionName != \"\"'> " +
            "AND r.name like CONCAT('%', #{regionName}, '%')" +
            "</if> " +
            "</where> " +
            "GROUP BY r.id, r.name, r.remark " +
            "</script>")
    @Results({
            @Result(property="id", column="id"),
            @Result(property="name", column="name"),
            @Result(property="remark", column="remark"),
            @Result(property="nodeCount", column="node_count", javaType=Integer.class)
    })
    Page<Map<String, Object>> selectRegionList(Page<?> page,String regionName);

    /**
     * 根据ID查询运营区域信息
     *
     * @param regionId 运营区域ID
     * @return 运营区域实体
     */
    @Select("SELECT id, name, remark FROM tb_region WHERE id = #{regionId}")
    RegionEntity selectRegionById(Long regionId);
}